<template>
  <div class="item-config">
        <el-button
            type="primary"
            style="width:165px;"
            size="small"
            @click="startAuth"
            >{{ $t('custom.connector.syncAuthorization') }}</el-button>
  </div>
</template>

<script>
import { Component } from './modules/Component.js'
import Trail from '@/components/storybook/mixins/trail'

export default {
  mixins: [Trail],

  props: {
    xeroConfigForm: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      testLoading: false
    }
  },
  methods: {
    startAuth () {
      let clientID = 'C3BC34F2CEE64346A32A6DC5F0946576'
      let url = window.location.origin
      const xeroAuthURL = 'https://login.xero.com/identity/connect/authorize' +
          '?client_id=' + clientID +
          '&redirect_uri=' + url + '/web/um/xero/redirect' +
          '&response_type=code&scope=offline_access accounting.settings.read accounting.reports.read accounting.contacts.read accounting.transactions' +
          '&state=' + this.$store.state.user.tenantCode + '_' + this.$store.state.user.id
      window.open(xeroAuthURL, '_blank')
    },
    validateConfigForm () {
      return this.$refs['xero-form'].validate()
    },
    synctdData (itemInfo) {
      const params = {
        tenantId: this.$store.state.user.tenantCode,
        subjectCode: 'XERO'
      }
      this.$interface.taskManager.syncClientData(params).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (itemInfo && res.data && res.data.sync) {
          itemInfo.sync.lastTime = res.data.sync.lastTime
          itemInfo.sync.status = res.data.sync.status
        }
        Component.$emit('getConnectorConfig')
        return this.$message.success(res.msg)
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped></style>
